Quick start¶
This section will show you how to get started using Python to call Stata. Stata provides a Python package called pystata that defines functions and introduces magic commands that allow you to interact with Stata from within Python. We demonstrate how this package works by executing code in a Jupyter Notebook, but you can choose to work in any IPython environment.
The Python package pystata is shipped with Stata and located in the utilities subdirectory of Stata’s installation directory. First, you need to configure this package within Python so that it can be found and imported by Python. The Configuration section contains details about different ways you can configure this package; below, we will be using the first method listed there. In the first method, the configuration module stata_setup, which is available in the Python Package Index (PyPI), is provided to locate the pystata package to initialize Stata.
Suppose we have Stata installed in C:\Program Files\Stata17\ and we use Stata/MP edition. Stata can be initialized as follows:
[1]:
import stata_setup
stata_setup.config("C:/Program Files/Stata17/", "mp")
___ ____ ____ ____ ____ ®
/__ / ____/ / ____/ 17.0
___/ / /___/ / /___/ MP—Parallel Edition
Statistics and Data Science Copyright 1985-2021 StataCorp LLC
StataCorp
4905 Lakeway Drive
College Station, Texas 77845 USA
800-STATA-PC https://www.stata.com
979-696-4600 [email protected]
Stata license: 10-user 4-core network perpetual
Serial number: 1
Licensed to: Stata Developer
StataCorp LLC
Notes:
1. Unicode is supported; see help unicode_advice.
2. More than 2 billion observations are allowed; see help obs_advice.
3. Maximum number of variables is set to 5,000; see help set_maxvar.
If Stata is initialized successfully, you will see output similar to that shown above.